<template>
	<view class="container">
<!-- 		platform: {{platform}}<br>
		
		nickName: {{nickName}}<br>
		avatarUrl: {{avatarUrl}}<br> -->
		code: {{code}}<br>
		openid: {{openid}}<br>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				platform: '',
				openid: '',
				nickName: '',
				avatarUrl: '',
				code:''
			}
		},
		created (){
			this.wxLogin()
		},
		methods: {
			wxLogin() {
				//漫路h
				uni.login({
				   success: res => {
				     //code值(5分钟失效)
				     console.info(res.code);
					 this.code=res.code
				     uni.request({
				        // 改成自己的服务地址
				        url:'https://api.weixin.qq.com/sns/jscode2session',
						data: {
							appid: 'wx8a65e2f8d9c668d2',
							secret: 'c3105a47c0bc6ba7ef9d9d2344c28779',
							js_code: this.code,
							grant_type: 'authorization_code'
						},
				        method:'GET',
				        success: (res) => {
				           // 这里就拿到openid了,不过一般都是直接在后端使用了,不需要拿到前端了,我就是为了做个演示。
						   console.log('res: '+ JSON.stringify(res))
						   console.log('res.openid: '+res.data.openid)
				           this.openid=res.data.openid
				        }
				     })
				   }
				});
			}
		}
	}
</script>

<style>
	.container {
		padding: 20px;
		font-size: 14px;
		line-height: 24px;
	}
</style>
